import Vue from "vue";
import Vuerouter from "vue-router";
import routes from "./routers"
import store from "@/store"

// import Home from "@/pages/Home";
// import Song from "@/pages/Song";
// import PlayLists from "@/pages/PlayLists";
// import AlbumPage from "@/pages/AlbumPage";
// import Artist from "@/pages/Artist";
// import UserHome from "@/pages/UserHome";

// 使用懒加载
Vue.use(Vuerouter);

const router = new Vuerouter({
  routes,
  scrollBehavior(to, from, savedPosition) {
    // 始终滚动到顶部
    return { y: 0 };
  },

});


router.beforeEach(async (to, from, next) => {
  //to 去哪里 from 来自哪里 next 放行  next（/home）直接强制跳转
  let token = store.state.User.token; //token标识
  if (token) {
      next();
  } else {

    console.log(to.path == "/my");
    console.log("未登录");
    if(to.path == "/my"){
      next();
    }
    // 暂时没解决错
    if(to.path == "/m"|| to.path.indexOf("/userlevel")  != -1 || to.path.indexOf("/userupdate")  != -1 || to.path.indexOf("/mymusic") != -1 ){
      next('/home');
    }
    next()
  }
});


export default router